﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIController : MonoBehaviour {

    public static UIController _instance;
    private int i_AllUIChildCount;
    public Transform tf_AllUI;
    public Color32 c_hitColor = new Color32(255,238,255,255);
    public Color32 c_hitColorText = new Color32(182,159,186,255);
    public Color32 c_notHitColor = new Color32(255,238,255,0);
    public Color c_notHitColorText = Color.white;

    public GameObject go_zhishideng;

    // Use this for initialization
    void Start () {
        _instance = this;
        EventManager<bool>.AddEvent(MyEventType.ResetButtonClickEvent,OnReset);
        EventManager<bool>.AddEvent(MyEventType.ChangeUIToShijianjiluEvent,changeMainUI);
        EventManager<string>.AddEvent(MyEventType.SiFangDirButtonClickEvent,OnDirButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SifangHeipingEvent,changeHeiping);
        i_AllUIChildCount = tf_AllUI.childCount;
    }

    /// <summary>
    /// 黑屏显示与消失
    /// </summary>
    /// <param name="active"></param>
    private void changeHeiping(bool active)
    {
        if (active) {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "heiping")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
            go_zhishideng.SetActive(!active);
        }
        else
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "heiping")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        tf_AllUI.GetChild(i).SetAsFirstSibling();
                    }).Start(0.1f);
                    break;
                }
            }
            go_zhishideng.SetActive(!active);
        }
    }

    private void OnDirButtonClick(string name)
    {
        switch (name) {
            case "up":OnUpButtonClick();break;
            case "down":OnDownButtonClick();break;
            case "left":OnLeftButtonClick();break;
            case "right":OnRightButtonClick();break;
            default:break;
        }
    }

    public void ChangeGaoliang(Transform tf, int index,bool active) {
        if (!active) {
            tf.GetChild(index).GetComponent<Image>().color = c_notHitColor;
            tf.GetChild(index).GetChild(0).GetComponent<Text>().color = c_notHitColorText;
        }
        else {
            tf.GetChild(index).GetComponent<Image>().color = c_hitColor;
            tf.GetChild(index).GetChild(0).GetComponent<Text>().color = c_hitColorText;
        }
    }

    void FixedUpdate() {
        TimerMngr.Instance.FixedUpdate();
    }

    private void OnUpButtonClick()
    {
        EventManager<bool>.TriggerEvent(MyEventType.UpButtonClickEvent,true);
    }

    private void OnDownButtonClick()
    {
        EventManager<bool>.TriggerEvent(MyEventType.DownButtonClickEvent,true);
    }

    private void OnLeftButtonClick()
    {
        EventManager<bool>.TriggerEvent(MyEventType.LeftButtonClickEvent,true);
    }

    private void OnRightButtonClick()
    {
        EventManager<bool>.TriggerEvent(MyEventType.RightButtonClickEvent,true);
    }

    /// <summary>
    /// 每次实验完后需点击信号复归方可再次实验且三段电流都需为“投入”状态
    /// </summary>
    /// <param name="active"></param>
    void OnReset(bool active) {
        int sum = 0;
        for (int i = 3; i <= 5; i++) {
            if (YabanShezhiUIController._instance.tf_ContentRight.GetChild(i).GetChild(0).GetComponent<Text>().text == "投入") {
                sum++;
            }
        }
        if (sum == 3) {//三段都是投入状态
            changeZhishideng();
            EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent,true);
            ShijianJiluUIController._instance.b_canStart = true;
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "mainUI")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
        }
    }
    /// <summary>
    /// 每次实验完后指示灯变红，按下信号复归后指示灯为投入状态（绿色）
    /// </summary>
    void changeZhishideng()
    {
        for (int index = 1; index <= 3; index++)
        {
            Unit unit = new Unit(index, "touru");
            EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent, unit);
        }
    }
    /// <summary>
    /// 改变当前界面为“事件记录”界面
    /// </summary>
    /// <param name="active"></param>
    void changeMainUI(bool active)
    {
        if (active)
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "shijianjiluGO")
                {
                    TimerMngr.Instance.Create(() =>
                    {
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
        }
    }
}
